import {Component, OnInit, ViewChild} from '@angular/core';
import {tuikeSystemUrl} from "../../../../config/api";
import {Router} from "@angular/router";
import {CategoryService} from "../../category/category.service";
import {HttpService} from "../../../lib/service/http.service";
import {Headers, RequestOptions} from "@angular/http";
import {ModalDirective} from "ngx-bootstrap";
import {LoginService} from "../../../lib/service/tuike-login.service";

@Component({
  selector: 'app-user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.scss'],
  providers: [CategoryService]
})
export class UserComponent implements OnInit {

  _isSpinning = false;
  pageSize = 20;
  page = 1;
  totalItems = 0;
  keywordList = [];
  keywordTotal = 0;
  categories = [];
  addStatus = false;
  type = 'error';
  isShow = false;
  message = '';
  editKey = '';
  editId = '';
  keyCategory = '1';
  canVisit = false;

  @ViewChild('confirmModal') public confirmModal: ModalDirective;


  constructor(private router: Router, private httpService: HttpService, private categoryService: CategoryService,private loginService: LoginService) {

  }


  ngOnInit() {
    this.canVisit = this.loginService.canVisitCheck('add_user');
    this.getHotKeyList(1);
  }

  goKeyword() {
    this.router.navigate(['tuike/keyword/add']);
  }


  addHotKeyWord() {
    const url = `${tuikeSystemUrl}/api/v1/admin/task_hot_key_show/add_key`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({'key':this.editKey, 'sort': 999, 'task_search_hot_key_id' : this.editId});
    this.addStatus = true;
    this.httpService.httpPostAuth(url, body, options).subscribe(rs => {
      if (rs.code === 200) {
        this.type = 'success';
        this.message = '添加成功';
        this.isShow = true;
        setTimeout(() => {
          this.addStatus = false;
          this.router.navigate(['tuike/keyword/hot']);
        }, 500);
      } else {
        this.addStatus = false;
        this.type = 'error';
        this.message = rs.message;
        this.isShow = true;
      }
    })
  }

  addTaskKeyWord() {
    const url = `${tuikeSystemUrl}/api/v1/admin/task_key/add_key`;
    const headers = new Headers({'Content-Type': 'application/json'});
    const options = new RequestOptions({headers: headers});
    const body = JSON.stringify({'key':this.editKey, 'sort': 999, 'task_search_hot_key_id' : this.editId});
    this.addStatus = true;
    this.httpService.httpPostAuth(url, body, options).subscribe(rs => {
      if (rs.code === 200) {
        this.type = 'success';
        this.message = '添加成功';
        this.isShow = true;
        setTimeout(() => {
          this.addStatus = false;
          this.router.navigate(['tuike/keyword/list']);
        }, 500);
      } else {
        this.addStatus = false;
        this.type = 'error';
        this.message = rs.message;
        this.isShow = true;
      }
    })
  }

  console(e) {
    console.log(e);
  }

  getHotKeyList(page) {
    this._isSpinning = true;
    const url = `${tuikeSystemUrl}/api/v1/admin/task_hot_key_show/tuike_search_hot_keys?page=${page}&pageSize=${this.pageSize}`;
    this.httpService.httpGetAuth(url).subscribe(res => {
      if (res.code === 200) {
        this.keywordList = res.data.data;
        this.totalItems = res.data.total;
        this._isSpinning = false;
      }
    })
  }

  goEdit(id) {
    this.router.navigate(['tuike/keyword/add'], {
      queryParams: {
        id: id
      }
    })
  }

  showConfirmModal(key,id) {
    this.editKey = key;
    this.editId = id;
    this.confirmModal.show();
  }

  pageChanged(event: any): void {
    window.scrollTo(0, 0);
    this.getHotKeyList(event.page);
  }
}
